package com.wnxy.pms.order.core.mq;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rabbitmq.client.Channel;
import com.wnxy.pms.order.core.constant.OrderConstant;
import com.wnxy.pms.order.core.constant.RedisConstant;
import com.wnxy.pms.order.core.dto.OrderPayDto;
import com.wnxy.pms.order.core.entity.Order;
import com.wnxy.pms.order.core.service.IOrderService;
import com.wnxy.pms.order.core.vo.OrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
@Slf4j
public class OrderConsumer {
    @Autowired
    private IOrderService orderService;
    @Autowired
    private RedisTemplate redisTemplate;

    @RabbitHandler
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "order-dead-letter-queue",durable = "true",
                    autoDelete = "false",exclusive = "false"),
            exchange = @Exchange(value = "order-dead-letter-exchange",durable = "true",
                    autoDelete = "false",type = ExchangeTypes.TOPIC),
            key = "dead.letter.order")
    )
    public void handleOrder(@Payload OrderPayDto orderPayDto,Channel channel,
                            @Headers Map headers){
            log.info("消费死信队列中的超时消息");
            Long tag = (Long)headers.get(AmqpHeaders.DELIVERY_TAG);
            //判断从mq中获取到的超时数据，订单的状态是否为待支付，如果为待支付状态，则更新订单失效。
            OrderVo orderVo = orderService.findByOrderSn(orderPayDto.getOrderSn());
            if(orderVo.getStatus().equals(OrderConstant.ORDER_STATUS_UNPAY)){//0待付款
                log.info("订单失效！修改订单状态");
                LambdaUpdateWrapper<Order> updateWrapper = Wrappers.lambdaUpdate();
                updateWrapper.set(Order::getStatus,OrderConstant.ORDER_STATUS_INVALID);//已失效
                updateWrapper.eq(Order::getOrderSn,orderPayDto.getOrderSn());
                orderService.update(updateWrapper);
                //将redis中扣减的库存，还原
                redisTemplate.opsForList().leftPush(
                        RedisConstant.SECKILL_COUNT+orderPayDto.getPromotionId(),
                        orderPayDto.getProductId());
            }
            //签收
        try {
            channel.basicAck(tag,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
